import numpy as np
from numpy import array
from scipy import linalg
from datetime import datetime
from time import strftime
import random
import csv

'''
For proj march21
add a field to identify the cancer in population point shapefile

***revision of test12.py***
randomly choose the point from shapefile to add cancer
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_point_value_lists():
    data = []
    fn = points_CSV

    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        temp = int(float(record["FID_2"]))
        data.append(temp)
        
    return data

def cal_cancer_case(iLen):
    #add cancer
    #print "========================Add Cancer============================="
    #incidence = [0.010,0.015,0.005,0.015,0.015,0.005,0.005,0.015]

    #[high rate, low rate, relative high rate, relative low rate]    
    incidence = [0.003, 0.010, 0.017]
    
    sigma = 0.0010

    TotalPop = len(points_data)
    #print TotalPop
    #cancer = 0 #total of cancer case
    i = 0
    regCancer = np.fromfunction(generate_zero_list, (iLen,))
    
    while i < TotalPop:
        # if the point is within the relative high risk area
        #print int(points_data[i])
        #int(TP_data[int(points_data[i])])
        if points_data[i] in HighAreaID:
            iIncidence = 2
        elif points_data[i] in LowAreaID:
            iIncidence = 0
        else:
            iIncidence = 1

        #iIncidence = areaData[i][1]
        tempIncidence = np.random.normal(incidence[iIncidence], sigma, 1)
        if np.random.random() < tempIncidence[0]:
            #areaData[i][2] = 1
            regCancer[points_data[i]] = regCancer[points_data[i]] + 1  #total of cancer case
        i = i + 1
    return regCancer

def generate_zero_list(i):
    return i-i

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "======================================================="
    print "begin at " + getCurTime()
    #inputs = get_inputs()
    points_CSV = "c:/oneMillionTP1000.csv"
    #points_CSV = "C:/_DATA/CancerData/test/Feb10/1m_point_TP1000.csv"  # points with its Thessien Polygon ID
    HighAreaID = [8,16,844,915,919,921,923,924,63,265,267,268,333,336,337,339,340,342,343,348,13,174,178,198,886,887,888,889,890,69,70,87,88,369,370,372,442,443]

    #polygon id within low risk area 
    #LowAreaID = [5,103,106,513,517,518,520,531,534,535,536,541,146,171,182,810,811,814,815,864,867,20,133,692,694,695,696,698,702,705]
    # row 0, 1, 2, 3 are 10,000 pop; row 4, 5, 6, 7 are 20,000 pop
    '''
    HighAreaID = np.array([105, 410, 412, 413, 515, 516, 518, 520, 524, 527, 528,
                           53, 60, 291, 292, 293, 294, 295, 311,
                           19, 135, 145, 146, 609, 713,
                           170, 180, 181, 864, 865, 867, 873, 893, 897, 901, 902, 903, 982, 983,
                           48, 49, 69, 70, 88, 273, 274, 326, 331, 364, 367, 368, 446,
                           150, 151, 156, 157, 785, 786, 787, 788, 789, 790, 793, 794, 795, 796, 801, 802, 803, 804,
                           24, 109, 484, 490, 491, 492, 493, 495, 499, 556, 557, 558, 559, 560, 561, 563, 567, 628,
                           16, 168, 185, 831, 844, 847, 848, 850, 851, 852, 853, 915, 916, 918, 919, 920, 923])
    '''

    '''
    105, 410, 412, 413, 515, 516, 518, 520, 524, 527, 528
    53, 60, 291, 292, 293, 294, 295, 311
    19, 135, 145, 146, 609, 713
    170, 180, 181, 864, 865, 867, 873, 893, 897, 901, 902, 903, 982, 983
    48, 49, 69, 70, 88, 273, 274, 326, 331, 364, 367, 368, 446
    150, 151, 156, 157, 785, 786, 787, 788, 789, 790, 793, 794, 795, 796, 801, 802, 803, 804
    24, 109, 484, 490, 491, 492, 493, 495, 499, 556, 557, 558, 559, 560, 561, 563, 567, 628
    16, 168, 185, 831, 844, 847, 848, 850, 851, 852, 853, 915, 916, 918, 919, 920, 923
    '''
    points_data = build_point_value_lists()
    
    dis_reg = set(points_data)
    regCancerT = np.arange(0, len(dis_reg), 1)
    
    i = 0
    iterNum = 100
    while i < iterNum:
        print i
        i = i + 1
        random.shuffle(points_data)
        temp = cal_cancer_case(len(dis_reg))                               
        regCancerT = np.append(regCancerT, temp)
    regCancerT.shape = (iterNum + 1, -1)
    #temp = regCancerT.transpose
    temp = np.transpose(regCancerT)
    #print temp
    np.savetxt("C:/_DATA/CancerData/test/Jan15/TP1000_1m_17_03.csv", temp, delimiter=',')
    
    print "end at " + getCurTime()
